/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmTechnical.java
 *
 * Created on May 23, 2011, 8:14:03 AM
 */

package Technical;

import Connections.DataTools;
import Login.frmLogin;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.text.MaskFormatter;

/**
 *
 * @author BachDEV
 */
public class frmTechnical extends javax.swing.JFrame {

    JTable tblComp = null;
    Vector vec = null;
    Login.frmLogin login;

    /** Creates new form frmTechnical */
    public frmTechnical(frmLogin login) {

        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(InstantiationException ex){
            ex.printStackTrace();
        }catch(UnsupportedLookAndFeelException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }catch(IllegalAccessException ex){
            ex.printStackTrace();
        }
        try
        {
            initComponents();
            this.login = login;
            DataTools dt = new DataTools();
            showComplain(dt.getComplain());
            Dimension ds = Toolkit.getDefaultToolkit().getScreenSize();
            this.setLocation((ds.width - this.getWidth()) / 2, (ds.height - this.getHeight()) / 2);
            btnUpdate.setEnabled(false);
            this.login.setVisible(false);
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    /////////////////////////////////////////////////////////////////////////
      public void showComplain(Vector result)
    {
        try
        {
            Vector ve = new Vector();
            ve.add("ID");
            ve.add("Discription");
            ve.add("Customer Name");
            ve.add("Product Name");
            ve.add("Date receive");
            ve.add("Date fixed");
            ve.add("Status");
            DataTools dt = new DataTools();
            tblComp = new JTable(result, ve);
            tblComp.getColumnModel().getColumn(0).setMinWidth(0);
            tblComp.getColumnModel().getColumn(0).setMaxWidth(0);
            scrTechnical.setViewportView(tblComp);
            tblComp.addKeyListener(new KeyListener() {

                public void keyTyped(KeyEvent e) {

                }

                public void keyPressed(KeyEvent e) {

                }

                public void keyReleased(KeyEvent e) {
                    if(tblComp.getSelectedRow()  != -1)
                    {
                        btnUpdate.setEnabled(true);
                        int row = tblComp.getSelectedRow();
                        vec = new Vector();
                        for(int i = 0; i< 7; i++)
                        {
                            vec.add(tblComp.getValueAt(row, i));
                        }
                    }
                }
            });
            tblComp.addMouseListener(new MouseListener() {

                public void mouseClicked(MouseEvent e) {
                    btnUpdate.setEnabled(true);
                    int row = tblComp.getSelectedRow();
                    vec = new Vector();
                    for(int i = 0; i< 7; i++)
                    {
                        vec.add(tblComp.getValueAt(row, i));
                    }
                }

                public void mousePressed(MouseEvent e) {

                }

                public void mouseReleased(MouseEvent e) {

                }

                public void mouseEntered(MouseEvent e) {

                }

                public void mouseExited(MouseEvent e) {

                }
            });
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        scrTechnical = new javax.swing.JScrollPane();
        jLabel3 = new javax.swing.JLabel();
        cbSearchBy = new javax.swing.JComboBox();
        try
        {
            txfDateStart = new javax.swing.JFormattedTextField(new MaskFormatter("####/##/##"));
            btnUpdate = new javax.swing.JButton();
            jLabel4 = new javax.swing.JLabel();
            jLabel2 = new javax.swing.JLabel();
            txfDateEnd = new javax.swing.JFormattedTextField();
            btnSearch = new javax.swing.JButton();
            jButton1 = new javax.swing.JButton();
            jMenuBar1 = new javax.swing.JMenuBar();
            jMenu1 = new javax.swing.JMenu();
            mnuLogout = new javax.swing.JMenuItem();
            mnuExit = new javax.swing.JMenuItem();
            jMenu2 = new javax.swing.JMenu();
            jMenuItem3 = new javax.swing.JMenuItem();

            setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
            setTitle("Technical");

            jLabel5.setFont(new java.awt.Font("Times New Roman", 1, 20));
            jLabel5.setText("Technical Support");

            jLabel3.setText("Search By:");

            cbSearchBy.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Date receive", "Date Fixed" }));

        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

        btnUpdate.setText("Update Status");
        btnUpdate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdateActionPerformed(evt);
            }
        });

        jLabel4.setText("(yyyy/MM/dd)");

        jLabel2.setText("Date end:");

        try
        {
            txfDateEnd = new JFormattedTextField(new MaskFormatter("####/##/##"));
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

        btnSearch.setText("Search");
        btnSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSearchActionPerformed(evt);
            }
        });

        jButton1.setText("   Exit");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jMenu1.setText("File");

        mnuLogout.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_L, java.awt.event.InputEvent.CTRL_MASK));
        mnuLogout.setText("Logout");
        mnuLogout.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuLogoutActionPerformed(evt);
            }
        });
        jMenu1.add(mnuLogout);

        mnuExit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        mnuExit.setText("Exit");
        mnuExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                mnuExitActionPerformed(evt);
            }
        });
        jMenu1.add(mnuExit);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Help");

        jMenuItem3.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
        jMenuItem3.setText("Help");
        jMenuItem3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem3ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem3);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(scrTechnical, javax.swing.GroupLayout.PREFERRED_SIZE, 851, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(btnUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(18, 18, 18)
                                .addComponent(cbSearchBy, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(45, 45, 45)
                                .addComponent(txfDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jLabel4)
                                .addGap(18, 18, 18)
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(txfDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(btnSearch, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addGap(75, 75, 75)
                                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGap(36, 36, 36)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(329, 329, 329)
                    .addComponent(jLabel5)
                    .addContainerGap(390, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addComponent(scrTechnical, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txfDateStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel2)
                    .addComponent(txfDateEnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnSearch)
                    .addComponent(jLabel3)
                    .addComponent(cbSearchBy, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnUpdate)
                    .addComponent(jButton1))
                .addContainerGap(37, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabel5)
                    .addContainerGap(449, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void mnuLogoutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuLogoutActionPerformed
        int option = JOptionPane.showConfirmDialog(this, "Do you want to log out?", "Confirm", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (option == JOptionPane.OK_OPTION) {
            dispose();
            login.setVisible(true);
            login.pwdPassword.setText("");
            login.txtUserName.setText("");
        }
}//GEN-LAST:event_mnuLogoutActionPerformed

    private void mnuExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_mnuExitActionPerformed
        int n = JOptionPane.showConfirmDialog(this, "Are you exit?", "Config", 0, 3);
        if(n == 0)
            System.exit(-1);
}//GEN-LAST:event_mnuExitActionPerformed

    private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem3ActionPerformed
        // TODO add your handling code here:
        try {
            Runtime.getRuntime().exec("hh.exe " + "eProject.chm");
        } catch (IOException ex) {
        }
}//GEN-LAST:event_jMenuItem3ActionPerformed

    private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdateActionPerformed
        try {
            Date now = new Date(System.currentTimeMillis());
            SimpleDateFormat sdf  = new SimpleDateFormat("yyyy/MM/dd");
            String today = sdf.format(now);
            Vector ve = new Vector();
            ve.add(vec.get(0));
            ve.add(today);
            ve.add(vec.get(6));
            System.out.println("vec: " + ve.get(2));
            DataTools dt = new DataTools();
            int n = dt.updateComp(ve);
            if(n != 0) {
                JOptionPane.showMessageDialog(this, "You are update successfull");
                showComplain(dt.getComplain());
            }
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
}//GEN-LAST:event_btnUpdateActionPerformed

    private void btnSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSearchActionPerformed
        try {
            Vector ve = new Vector();
            DataTools dt = new DataTools();
            btnUpdate.setEnabled(false);
            ve.add(cbSearchBy.getSelectedItem());
            ve.add(txfDateStart.getText());
            ve.add(txfDateEnd.getText());
            if(dt.searchComp(ve).size() != 0) {
                showComplain(dt.searchComp(ve));
            } else {
                JOptionPane.showMessageDialog(this, "Complain is not found");
            }
        } catch(Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
}//GEN-LAST:event_btnSearchActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        int n = JOptionPane.showConfirmDialog(this, "Are you exit?", "Config", 0, 3);
        if(n == 0)
            System.exit(-1);
}//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new frmTechnical(null).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSearch;
    private javax.swing.JButton btnUpdate;
    private javax.swing.JComboBox cbSearchBy;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem3;
    private javax.swing.JMenuItem mnuExit;
    private javax.swing.JMenuItem mnuLogout;
    private javax.swing.JScrollPane scrTechnical;
    private javax.swing.JFormattedTextField txfDateEnd;
    private javax.swing.JFormattedTextField txfDateStart;
    // End of variables declaration//GEN-END:variables

}
